package com.bibrApp.difu.actvity.order_detail_mvp;

import android.content.Context;

import com.bibrApp.difu.Response.BannerResBody;
import com.bibrApp.difu.Response.CoverShortResBody;
import com.bibrApp.difu.Response.OrderDetailResBody;
import com.bibrApp.difu.Response.OrderMoreDetailResBody;
import com.bibrApp.difu.Response.SignDataResBody;
import com.bibrApp.difu.Response.SignIVDataResBody;
import com.bibrApp.difu.actvity.others.IOtherView;
import com.bibrApp.difu.api.RetrofitNet;
import com.bibrApp.difu.base.BaseObserver;
import com.bibrApp.difu.base.BasePresenter;
import com.bibrApp.difu.utils.EncryptUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.google.gson.Gson;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class OrderDetailPresenter extends BasePresenter<IOrderDetailView> {
    public OrderDetailPresenter(Context context, IOrderDetailView iView) {
        super(context, iView);
    }


    public void getData(String orderNo) {
        RetrofitNet.getInstance()
                .getApi()
                .getTradeDetail(EncryptUtils.encryptString(orderNo))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            String jsonStr = EncryptUtils.objectToJson(value);
                            OrderDetailResBody orderDetailResBody = new Gson().fromJson(jsonStr, OrderDetailResBody.class);
                            iView.successOfGetDetail(orderDetailResBody);
                        }
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                    }
                });
    }

    public void getDataDetail(String orderNo) {
        RetrofitNet.getInstance()
                .getApi()
                .getMoreTradeDetail(EncryptUtils.encryptString(orderNo))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            String jsonStr = EncryptUtils.objectToJson(value);
                            OrderMoreDetailResBody orderDetailResBody = new Gson().fromJson(jsonStr, OrderMoreDetailResBody.class);
                            iView.successOfGetMoreDetail(orderDetailResBody);
                        }
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                    }
                });
    }

    //撤消
    public void revoke(String orderNo) {
        RetrofitNet.getInstance()
                .getApi()
                .revoke(EncryptUtils.encryptString(orderNo))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            ToastUtils.ShowToast(context,"撤销成功");
                        }
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                    }
                });
    }

    //获取补仓信息
    public void getCoverShort(String orderNo) {
        RetrofitNet.getInstance()
                .getApi()
                .getCoverShort(EncryptUtils.encryptString(orderNo))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            String jsonStr = EncryptUtils.objectToJson(value);
                            CoverShortResBody orderDetailResBody = new Gson().fromJson(jsonStr, CoverShortResBody.class);
                            iView.successOfGetCoverShort(orderDetailResBody);
                        }
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                    }
                });
    }

    //获取签名数据
    public void getSignData(String orderNo) {
        RetrofitNet.getInstance()
                .getApi()
                .getSignData(EncryptUtils.encryptString(orderNo))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            String jsonStr = EncryptUtils.objectToJson(value);
                            SignDataResBody signDataResBody = new Gson().fromJson(jsonStr, SignDataResBody.class);
                            iView.successOfGetSignData(signDataResBody);
                        }
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                    }
                });
    }

    //通过hash获取签名数据
    public void getSignDataByHash(String hash) {
        RetrofitNet.getInstance()
                .getApi()
                .getSignDataByHash(EncryptUtils.encryptString(hash))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            String jsonStr = EncryptUtils.objectToJson(value);
                            SignIVDataResBody signIVDataResBody = new Gson().fromJson(jsonStr,SignIVDataResBody.class);
                            iView.successOfGetSignIVData(signIVDataResBody);
                        }
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                    }
                });
    }
}
